
import { Gravity, Mass, Acceleration, Resistince, Velocity, Impulse, ForceMode, getAccelerationByForce } from '../Base/Physics'
import Vector2 from '../Base/Vector2';
import Vertor2 from '../Base/Vector2'

interface IRigidbody {
    gravity: Gravity;
    mass: Mass;
    acceleration: Acceleration;
    resistince: Resistince;
    velocity: Velocity;
    position: Vector2;
}

export default class Rigidbody {
    gravity: Gravity;
    mass: Mass;
    acceleration: Acceleration;
    resistince: Resistince;
    velocity: Velocity;
    position: Vector2;
    constructor(options: IRigidbody) {
        this.gravity = options.gravity || 9.81;
        this.mass = options.mass
        this.velocity = options.velocity;
        this.resistince = options.resistince || 0;
        this.acceleration = options.acceleration || new Vector2(0, 0);
        this.position = options.position || new Vector2(0, 0);
    }
    addForce(vec: Vertor2, forceMode: ForceMode) {
        this.checkVelocityDirection(vec)
        switch (forceMode) {
            case ForceMode.Force:
                this.acceleration = getAccelerationByForce(vec, this.mass);
                break;
            case ForceMode.Acceleration:
                this.acceleration = vec;
                break;
            case ForceMode.Velocity:
                this.velocity = vec;
                break;
            // case ForceMode.Impulse:
            //     this.xAcceleration = getAccelerationByForce(vec.x, this.mass)
            //     this.yAcceleration = getAccelerationByForce(vec.y, this.mass)
            //     break;
        }
    }
    checkHorizontalDirection(vec: Vector2) {
        if (vec.x / this.velocity.x < 0) {
            return true
        } else {
            return false
        }
    }
    checkVerticalDirection(vec: Vector2) {
        if (vec.y / this.velocity.y < 0) {
            return true
        } else {
            return false
        }
    }
    checkVelocityDirection(vec: Vector2) {
        if (this.checkHorizontalDirection(vec)) {
            this.velocity = new Vector2(0, this.velocity.y)
        }
        if (this.checkVerticalDirection(vec)) {
            this.velocity = new Vector2(this.velocity.x, 0)
        }
    }
    move() {

        this.velocity = this.velocity.add(this.acceleration);
        this.position = this.position.add(this.velocity)
    }

}